package com.xiaotu.spider.baidu.pageprocessor;

import com.xiaotu.common.model.StarBaikeBirthdayModel;
import com.xiaotu.common.model.StarModel;
import com.xiaotu.common.model.StarStringModel;
import com.xiaotu.common.util.*;
import com.xiaotu.server.service.guduo.SupplementGuDuoRecordService;
import com.xiaotu.spider.AUrlPageProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @类名 StarBaikeInfoPageProcessor
 * @日期 2021年2月7日
 * @作者 王艳龙
 * @功能 百度百科艺人获奖记录
 */
@Component("StarBaikeBirthdayPageProcessor")
public class StarBaikeBirthdayPageProcessor extends AUrlPageProcessor
{
    private String start_url = "https://www.baidu.com/";

	private String filePath="D://";
	
    @Autowired
	private SupplementGuDuoRecordService starService;
    
    @Override
	protected String[] getStartRequestUrls() {
    	return new String[]{start_url};
	}
	
    @Override
   	public void process(Page page) {
    	if (Constants.PageDataType.STAR_RECORD.equals(this.getPageDataType(page)))
            this.tvRecord(page);
        else
            this.pageProcess(page);
    }
    
    private void pageProcess(Page page) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("rows",5000);
		param.put("page",1);
    	List<Map<String,Object>> starList = starService.queryStarBirthdayInfoList(param);
    	for (Map<String, Object> starInfoMap : starList) {
			String linkUrl = "https://baike.baidu.com/item/"+ KeywordEncode.encode(starInfoMap.get("starName")+"")+"?" +
					"starId="+starInfoMap.get("starId");
			PageUtils.addTargetRequest(page, linkUrl, page.getRequest().getExtras());
    	}
		/*String linkUrl = "https://baike.baidu.com/item/"+ KeywordEncode.encode("邓洁儿")+"?starId=1231323";
		PageUtils.addTargetRequest(page, linkUrl, page.getRequest().getExtras());*/
    }
    
	private void tvRecord(Page page) {
		String linkUrl = KeywordEncode.decode(page.getUrl().toString());
		String starName = null;
		String starId = null;
		starName = linkUrl.substring(linkUrl.indexOf("item/")+5, linkUrl.indexOf("?starId"));
		starId = linkUrl.substring(linkUrl.indexOf("starId=")+7, linkUrl.length());

		List<String> dtList = page.getHtml().xpath("//div[@class='basic-info']/dl/dt/text()").all();
		if (dtList.size() == 0) {
			dtList = page.getHtml().xpath("//div[@id='baseInfoWrapDom']/div/div/div/span/text()").all();
			if(dtList.size()==0){
				// 0：无百科，1:有百科无生日，2：有百科有生日
				page.putField(StarBaikeBirthdayModel.class.getName(), new StarBaikeBirthdayModel(starId,starName,0));
				page.putField("filePath", filePath);
				return;
			}
		}

		if(dtList.contains("出生日期")){
			// 0：无百科，1:有百科无生日，2：有百科有生日
			page.putField(StarBaikeBirthdayModel.class.getName(), new StarBaikeBirthdayModel(starId,starName,2));
			page.putField("filePath", filePath);
		}else{
			// 0：无百科，1:有百科无生日，2：有百科有生日
			page.putField(StarBaikeBirthdayModel.class.getName(), new StarBaikeBirthdayModel(starId,starName,1));
			page.putField("filePath", filePath);
		}
    }
    
	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.STAR_BAIKE_BIRTHDAY;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
        if (url.contains("item"))
        	return Constants.PageDataType.STAR_RECORD;
		return Constants.PageDataType.SEARCH_LIST;
	}

	/**
	 * 解析Baidu电视剧基本信息的dt和dd
	 *
	 * @param htmlStr
	 * @return
	 */
	public String getTextByHtml(String htmlStr) {
		// 过滤Baidu的标签
		while (htmlStr.contains("<sup>")) {
			String before = htmlStr.substring(0, htmlStr.indexOf("<sup>"));
			String after = htmlStr.substring(htmlStr.indexOf("</sup>") + 6, htmlStr.length());
			htmlStr = before + after;
		}
		return HtmlUtil.delHTMLTag(htmlStr);
	}

	/*public static void main(String[] args) throws ParseException {

		*//*Date date = new SimpleDateFormat("MM-dd").parse("06-09");
		String now = new SimpleDateFormat("MM月dd日").format(date);*//*

		Date date = new SimpleDateFormat("yyyy-MM-dd").parse("1983-12-12");
		String now = new SimpleDateFormat("yyyy年MM月dd日").format(date);

		System.out.println(now);


	}*/
}
